<?php
/**
 * (logic)（临时）Payment-Total总额
 */
namespace App\Logic\File\Profit;

use App\Models\MonthlyReport\Tmp\TmpPTotal;
class ParsePTotalFile
{
    /**
     * 解析PTotalFile
     * @param $lines
     * @param $date
     * @return int
     */
    public static function exeParse($lines,$date){
        $count = count($lines);
        $i = 0;
        $num = 100;
        $re_count = 0;
        while(true){
            if(empty($lines))break;
            $_lines = array_splice($lines,$i,$num);
            foreach ($_lines as $line){
                $flag = self::_addPTotal($line,$date);
                $flag && $re_count++;
            }
        }
        return $re_count == $count ? 1 : 0;
    }

    private static function _addPTotal($temp,$date){
        if(count($temp) < 4)return true;
        if(!$temp[0])return true;

        //Payment-Total总额（临时）
        $re = TmpPTotal::create([
            'report_date' => $date,
            'account' => strtoupper($temp[0]),
            'country' => strtoupper($temp[1]),
            'sku' => $temp[2],
            'total' => replaceAmountChar($temp[3]),
        ]);
        return !$re ? false : true;
    }
}
